package com.enfry.common.util.excel;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jdom.Element;

public class TD extends Cell {
	private static final long serialVersionUID = -6748363994670511284L;
	private String id;
	private String label;
	private int width = 1;
	private int hight = 1;
	private String dataType = "";
	private String dataFormat = "";
	private String validateRegex;
	private String hint;
	private String key;
	
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public String getDataFormat() {
		return dataFormat;
	}

	public void setDataFormat(String dataFormat) {
		this.dataFormat = dataFormat;
	}

	public String getValidateRegex() {
		return validateRegex;
	}

	public void setValidateRegex(String validateRegex) {
		this.validateRegex = validateRegex;
	}

	public String getHint() {
		return hint;
	}

	public void setHint(String hint) {
		this.hint = hint;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public void setHight(int hight) {
		this.hight = hight;
	}

	@Override
	public int getWidth() {
		return this.width;
	}

	@Override
	public int getHight() {
		return this.hight;
	}

	public static TD load(Element element) throws Exception {
		String colspan = element.getAttributeValue("colspan");
		String rowspan = element.getAttributeValue("rowspan");
		int width = 1;
		try {
			width = Integer.parseInt(colspan);
		} catch (Exception e) {
		}
		int hight = 1;
		try {
			hight = Integer.parseInt(rowspan);
		} catch (Exception e) {
		}
		TD td = new TD();
		td.setId(element.getAttributeValue("id"));
		td.setLabel(element.getAttributeValue("label"));
		td.setWidth(width);
		td.setHight(hight);
		td.setKey(element.getAttributeValue("key"));
		td.setValidateRegex(element.getAttributeValue("validateRegex"));
		td.setHint(element.getAttributeValue("hint"));
		if ((element.getAttributeValue("dataType") != null)
				&& (element.getAttributeValue("dataType").length() > 0)) {
			td.setDataType(element.getAttributeValue("dataType"));
		}
		if ((element.getAttributeValue("dataFormat") != null)
				&& (element.getAttributeValue("dataFormat").length() > 0)) {
			td.setDataFormat(element.getAttributeValue("dataFormat"));
			checkDataFormat(td);
		}
		return td;
	}

	private static void checkDataFormat(TD td) {
		String dataType = td.getDataType();
		String dataFormat = td.getDataFormat();
		if ("number".equalsIgnoreCase(dataType)) {
			Pattern p = Pattern.compile("0*\\.0*");
			Matcher m = p.matcher(dataFormat);
			boolean b = m.matches();
			if (!b) {
				throw new InvalidTDFormatException("cell" + td.getLabel()
						+ " Number format should be 0*.0*");
			}
		} else if ("date".equalsIgnoreCase(dataType)) {
			Pattern p = Pattern.compile("yyyy-MM-dd");
			Matcher m = p.matcher(dataFormat);
			boolean b = m.matches();
			if (!b) {
				p = Pattern.compile("yyyy-MM-dd\\sHH:mm:ss");
				m = p.matcher(dataFormat);
				b = m.matches();
			}
			if (!b) {
				throw new InvalidTDFormatException(
						"cell"
								+ td.getLabel()
								+ " Date format should be yyyy-MM-dd or yyyy-MM-dd HH:mm:ss");
			}
		}
	}

}
